﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Text;
using System.Linq;

namespace SP_XHWeb.ControlTemplates.SP_XHWeb
{
    public partial class StudyAndDevelopment : UserControl
    {
        private string DispFormUrl = "/_layouts/SP_XHWeb/DispFormItem.aspx?1=1";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblsad.InnerHtml = InitStudyAndDelevopment();
            }
        }

        private string InitStudyAndDelevopment()
        {
            StringBuilder str = new StringBuilder();
            List<SPListItem> groupLists = GetStudyAndDelevopment();
            //标题长度
            int len = 22;

            str.Append("<ul>");

            if (groupLists != null && groupLists.Count > 6)
            {
                groupLists = (from emp in groupLists orderby emp["创建时间"] descending select emp).Take(6).ToList<SPListItem>();
                for (int i = 0; i < 6; i++)
                {
                    string[] strs = groupLists[i]["Categories"].ToString().Split('_');
                    string linkUrl = "";
                    if (strs.Length == 3)
                    {
                        linkUrl = DispFormUrl + "&web_url=/" + strs[0] + "/" + strs[1] + "&list_title=" + strs[2] + "&item_id=" + groupLists[i]["SID"];
                    }
                    else if (strs.Length == 2)
                    {
                        linkUrl = DispFormUrl + "&web_url=/" + strs[0] + "&list_title=" + strs[1] + "&item_id=" + groupLists[i]["SID"];
                    }
                    //linkUrl = DispFormUrl + "&web_url=" + groupLists[i].ParentList.ParentWebUrl + "&list_title=" + groupLists[i].ParentList.Title + "&item_id=" + groupLists[i].ID;
                    str.Append("<li>");
                    if (groupLists[i].Title.Length > len)
                    {
                        str.AppendFormat("<a href=\"javascript:openDialog('{0}','培训通知与简报')\">{1}</a>", linkUrl, groupLists[i].Title.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<a href=\"javascript:openDialog('{0}','培训通知与简报')\">{1}</a>", linkUrl, groupLists[i].Title);
                    }
                    str.Append("</li>");
                }
            }
            else
            {
                groupLists = (from emp in groupLists orderby emp["创建时间"] descending select emp).ToList<SPListItem>();
                for (int i = 0; i < groupLists.Count; i++)
                {
                    string[] strs = groupLists[i]["Categories"].ToString().Split('_');
                    string linkUrl = "";
                    if (strs.Length == 3)
                    {
                        linkUrl = DispFormUrl + "&web_url=/" + strs[0] + "/" + strs[1] + "&list_title=" + strs[2] + "&item_id=" + groupLists[i]["SID"];
                    }
                    else if (strs.Length == 2)
                    {
                        linkUrl = DispFormUrl + "&web_url=/" + strs[0] + "&list_title=" + strs[1] + "&item_id=" + groupLists[i]["SID"];
                    }
                    //string linkUrl = DispFormUrl + "&web_url=" + groupLists[i].ParentList.ParentWebUrl + "&list_title=" + groupLists[i].ParentList.Title + "&item_id=" + groupLists[i].ID;
                    str.Append("<li>");
                    if (groupLists[i].Title.Length > len)
                    {
                        str.AppendFormat("<a href=\"javascript:openDialog('{0}','培训通知与简报')\">{1}</a>", linkUrl, groupLists[i].Title.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<a href=\"javascript:openDialog('{0}','培训通知与简报')\">{1}</a>", linkUrl, groupLists[i].Title);
                    }
                    str.Append("</li>");
                }
            }


            str.Append("</ul>");
            return str.ToString();
        }


        //资讯中心需要显示的信息
        private List<SPListItem> GetStudyAndDelevopment()
        {
            List<SPListItem> lists = new List<SPListItem>();
            //  http://
            string listUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            //using (SPSite site = new SPSite(listUrl + "/infomationcenter/default.aspx"))
            //{
            //    using (SPWeb web = site.OpenWeb())
            //    {
            //        SPList list = web.Lists.TryGetList("InfomationStudy");
            //        if (list != null && list.ItemCount != 0)
            //        {
            //            foreach (SPListItem item in list.Items)
            //            {
            //                if (item.Fields.ContainsField("首页显示"))
            //                {
            //                    if (item["首页显示"] != null && !string.IsNullOrEmpty(item["首页显示"].ToString()) && item["首页显示"].ToString() == "显示")
            //                    {
            //                        lists.Add(item);
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
            using (SPSite site = new SPSite(listUrl + "/approvestudy"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("ShowStudyList");
                    if (list != null && list.ItemCount != 0)
                    {
                        foreach (SPListItem item in list.Items)
                        {
                            if (item.Fields.ContainsField("是否显示"))
                            {
                                if (item["是否显示"] != null && !string.IsNullOrEmpty(item["是否显示"].ToString()) && item["是否显示"].ToString() == "显示" && item["Categories"].ToString().Contains("InfomationStudy"))
                                {
                                    lists.Add(item);
                                }
                            }
                        }
                    }
                }
            }
            return lists;
        }

        
    }
}
